﻿//-WinterFramework © WinterFeather
using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
using DG.Tweening;
using DG;
public class SingleMonoScene<T> : MonoBehaviour where T : MonoBehaviour
{
    public static T Instance
    {
        get
        {
            if (!instance)
            {
                GameObject g = new GameObject(typeof(T).Name);
                instance = GameObject.FindObjectOfType<T>();
                if (!instance) instance = g.AddComponent<T>();
                //g.hideFlags = HideFlags.HideInHierarchy;
            }
            return instance;
        }
    }
    static T instance;
}
public class SingleMono<T> : MonoBehaviour where T : MonoBehaviour
{
    public void Awake()
    {
        Init();
    }

    public static T Instance
    {
        get
        {
            if (!instance)
            {
                GameObject g = new GameObject(typeof(T).ToString());
                //GameObject.DontDestroyOnLoad(g);
                instance = GameObject.FindObjectOfType<T>();
                if (!instance) instance = g.AddComponent<T>();
                //g.hideFlags = HideFlags.HideInHierarchy;
            }
            return instance;
        }
    }
    static T instance;

    void Init()
    {
        GameObject.DontDestroyOnLoad(gameObject);
    }
}
public class SingleData<T> where T : class, new()
{
    static T instance;
    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new T();
            }
            return instance;
        }
    }
}

public static class BinarySerializationHelper
{
    public static byte[] SerializeToBinary(object obj)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(ms, obj);
            return ms.ToArray();
        }
    }

    public static T DeserializeFromBinary<T>(byte[] data)
    {
        using (MemoryStream ms = new MemoryStream(data))
        {
            BinaryFormatter bf = new BinaryFormatter();
            return (T)bf.Deserialize(ms);
        }
    }
}